package com.example.sistemacompras;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.example.sistemacompras.db.DatabaseHandler;
import com.example.sistemacompras.entity.Client;
import com.example.sistemacompras.entity.Order;

public class OrdersHistory extends Activity {
	private DatabaseHandler db;
	private Client mClient;
	private List<Order> mOrders;
	private ListView list;
	private CustomOrderAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orders_history);
		db = new DatabaseHandler(getApplicationContext());
		mClient = db.getClientById((Integer) getIntent().getExtras().get(
				"user_id"));
		mOrders = new ArrayList<Order>();
		if (mClient != null) {
			mOrders = db.getAllOrdersByClient(mClient);
		}
		
		adapter = new CustomOrderAdapter(this, mOrders, getResources());
		adapter.notifyDataSetChanged();
		list = (ListView) findViewById(R.id.cart_list_view);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {
				final Order order = mOrders.get(position);
				Intent intent = new Intent(getApplicationContext(),
						OrderDetailHistoryActivity.class);
				intent.putExtra("order_id", order.getId());
				startActivity(intent);
				

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.orders_history, menu);
		return true;
	}

}
